

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var user_id = user.id;
    var onoff = false;
    var pageonOff = true;//调用分页按钮的的开关
    $('.works_show ').removeClass('clearfix').addClass('resetInlineBlock');
    $('.all_sort').mouseenter(function () {
        $('.content_top_left').fadeIn("slow");
    }).mouseleave( function () {
        $('.content_top_left').fadeOut("slow");
    });
    $('.left a').click(function () {
        $(this).addClass('sort').siblings().removeClass('sort');
        current_type = $(this).attr('data-index');
        requestGoodsList(0);
    });
    var current_type = 1;
    requestGoodsList(0);
    function requestGoodsList(page) {
        var order_str = "";
        if (current_type = 1) order_str = null;else if (current_type = 2) order_str = "goods_price";else if (current_type = 3) order_str = "onsale_time";
        //可定制的商品列表
        var params = {
            "[]": {
                "query": 2,
                "count": 60,
                "page": page,
                "Goods": {
                    "@order": order_str,
                    "onsale_flag": 1,
                    "deleted_flag": 0,
                    "preview_flag": 0,
                    "gift_flag": 0,
                    "custom_flag": 1,
                    "GoodsImgs": {
                        "main_flag": 1,
                        "goods_id@": "/id"
                    }
                }
            },
            "total@": "[]/total"
        };
        send_ajax('post', '/get', params, function callback(res) {
            var html = "";
            if (res.code == 200&&res["[]"]) {
                var data = res["[]"];
                $('#number').text(res.total);
                for (var i = 0; i < data.length; i++) {
                    var a_goods = data[i].Goods;
                    if (a_goods) {
                        html += ' <li class="works_show_item" data-id="' + a_goods.id + '" data-title="' + a_goods.name + '" \n                        data-price="' + a_goods.goods_price + '"\n                        data-txt="' + a_goods.name + '">\n                                <a  target="_blank" href="/goods-' + a_goods.id + '.html" class="works_show_pic">\n                                    <img src="' + (undefined == a_goods.GoodsImgs ? "https://pic.90sjimg.com/back_pic/u/00/00/95/36/55e6adaaa0bb2.jpg" : isImgHttp(a_goods.GoodsImgs.img)) + '" alt="' + (undefined == a_goods.GoodsImgs ? "" : a_goods.GoodsImgs.alt) + '">\n                                    <i class="goods_tag_customization"></i>\n                                </a>\n                                <div class="msg">\n                                    <em class="em_halloffame ' + (a_goods.hof_flag == true ? 'inline' : 'hide') + '">\u540D\u4EBA\u5802</em>\n                                    <a target="_blank" href="/goods-' + a_goods.id + '.html">' + a_goods.name + '</a>\n                                </div>\n                                <ul class="works_show_item_bottom">\n                                    <li class="items">\n                                        <span class="my_customized">\n                                            <a href="http://114.116.69.140:8888/dialog_1.htm">\u4F5C\u54C1\u5B9A\u5236</a>\n                                        </span>\n                                    </li>\n                                    <li class="item contrast_box">\n                                        <span class="contrast"></span>\n                                        <span>\u5BF9\u6BD4</span>\n                                    </li>\n                                    <li class="item collect_box">\n                                        <i class="collect"></i>\n                                        <span>\u6536\u85CF</span>\n                                    </li>\n                                </ul>\n                            </li> ';
                    }
                }
                $('.works_show').html(html);
                if(pageonOff){
                    twbsPagination(res.total);
                    pageonOff=false;
                }
                isStore(); //渲染是不是有已经收藏的商品。
                //渲染已经加入的对比
                if ($.cookie('goods_list') != undefined) {
                    var localData = getLocalData();
                    if (localData == undefined) return;
                    for (var i = 0; i < localData.length; i++) {
                        var obj = localData[i];
                        var id = obj.productID;
                        $('[data-id=' + id + ']').find(".contrast").addClass('contrast_se');
                    }
                };
               
            } else {
                alert('网络加载失败，请稍后再试！');
            }
        });
    };

    function twbsPagination(total) {
         //分页按钮
            Page({
                num:Math.ceil(total / 60),					//页码数
                startnum:1,				//指定页码
                elem:$('#pagination_b'),		//指定的元素
                callback:function(page){	//回调函数
                    if (page != 1) {
                        onoff = true;
                    }
                    if (onoff) {
                        $('.works_show').html('');
                        requestGoodsList(page - 1);
                        $('html,body').animate({scrollTop:$('.s_content ').offset().top-80}, 500);
                    }
                }
            });
    };
    //是否已经收藏过的商品
    function isStore() {
        if (!user_id) {
            return;
        }
        var element = $('.works_show');
        var goodsIds = [];
        for (var k = 0; k < element.length; k++) {
            goodsIds.push($(element[k]).attr('data-id'));
        };
        var param = {
            "GoodsCollect[]": {
                "GoodsCollect": {
                    "goods_id{}": goodsIds,
                    "user_id": user_id
                }
            }
        };
        send_ajax('post', '/get', param, function callback(result) {
            if (result.code == 200 && result['GoodsCollect[]'] != undefined) {
                var data = result['GoodsCollect[]'];
                for (var j = 0; j < data.length; j++) {
                    $('[data-id=' + data[j].goods_id + ']').find(".collect").addClass('select_collect');
                }
            }
        });
    };
});